﻿function WaitForJobToFinish([string]$SolutionFileName)
{ 
	$JobName = "*solution-deployment*$SolutionFileName*"
	$job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
	if ($job -eq $null) 
	{
		Write-Host 'Timer job not found'
	}
	else
	{
		$JobFullName = $job.Name
		Write-Host -NoNewLine "Waiting to finish job $JobFullName"
		
		while ((Get-SPTimerJob $JobFullName) -ne $null) 
		{
			Write-Host -NoNewLine .
			Start-Sleep -Seconds 2
		}
		Write-Host  "Finished waiting for job.."
	}
}

function DeleteTimerJob([string]$SolutionFileName)
{ 
	$JobName = "*solution-deployment*$SolutionFileName*"
	$job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
	if ($job -ne $null) 
	{
		Write-Host 'Existing Timer job found. Deleting'
		$job.Delete()
	}
}

Write-Host 'Loading SharePoint'
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
$url=$args[0]
$featureID="aaaaaaaa-1111-5555-7777-ccccccccc201"
$solutionName="Smart.Documents.wsp"

$featureExists = Get-SPSolution $solutionName -ErrorAction SilentlyContinue

if ($featureExists)
{
	DeleteTimerJob($solutionName)


	Write-Host 'Going to disable feature: webparts'
	disable-spfeature -identity "aaaaaaaa-1111-5555-7777-ccccccccc205" -confirm:$false -url $url
 
	Write-Host 'Going to disable feature: docs'
	disable-spfeature -identity "aaaaaaaa-1111-5555-7777-ccccccccc201" -confirm:$false -url $url

	Write-Host 'Going to disable feature: templ'
	disable-spfeature -identity "aaaaaaaa-1111-5555-7777-ccccccccc204" -confirm:$false -url $url

	Write-Host 'Going to disable feature: ass'
	disable-spfeature -identity "aaaaaaaa-1111-5555-7777-ccccccccc202" -confirm:$false -url $url

	Write-Host 'Going to disable feature: os'
	disable-spfeature -identity "aaaaaaaa-1111-5555-7777-ccccccccc203" -confirm:$false -url $url

	Write-Host 'Going to uninstall feature: webparts'
	uninstall-spfeature -identity "aaaaaaaa-1111-5555-7777-ccccccccc205" -confirm:$false -force
 
	Write-Host 'Going to uninstall feature: docs'
	uninstall-spfeature -identity "aaaaaaaa-1111-5555-7777-ccccccccc201" -confirm:$false -force

	Write-Host 'Going to uninstall feature: templ'
	uninstall-spfeature -identity "aaaaaaaa-1111-5555-7777-ccccccccc204" -confirm:$false -force

	Write-Host 'Going to uninstall feature: ass'
	uninstall-spfeature -identity "aaaaaaaa-1111-5555-7777-ccccccccc202" -confirm:$false -force

	Write-Host 'Going to uninstall feature: os'
	uninstall-spfeature -identity "aaaaaaaa-1111-5555-7777-ccccccccc203" -confirm:$false -force

	Write-Host 'Going to uninstall solution'
	Uninstall-SPSolution -identity $solutionName  -allwebapplications -confirm:$false

	Write-Host 'Waiting for job to finish'
	WaitForJobToFinish($solutionName)

	Write-Host 'Going to remove solution'
	Remove-SPSolution –identity $solutionName -confirm:$false

	Write-Host 'Unloading SharePoint'
	Remove-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
}
else
{
	Write-Host 'Solution not installed'
}
